const fs = require('fs');
const express = require('express');
const router = express.Router();
const multer = require("multer");

// 创建文件夹
const createFolder = function (folder) {
    try {
        fs.accessSync(folder);
    } catch (e) {
        fs.mkdirSync(folder);
    }
};

// 定义上传路径和文件名
const storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, uploadFolder)
    },
    filename: function (req, file, cb) {
        cb(null, new Date().getTime() + file.originalname )
    }
});

const uploadFolder = 'D:\\image';
createFolder(uploadFolder);
const upload = multer({storage: storage});

// 多文件上传请求
router.post('/upload', upload.array('file', 10), function (req, res, next) {
    let fileNameArray = [];
    for (let i = 0; i < req.files.length; i++) {
        fileNameArray.push({'filename': req.files[i].filename})
    }
    res.json(fileNameArray);
});

module.exports = router;
